package com.cotte.config;

import org.apache.tomcat.jdbc.pool.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
public class DataSourceConfig {
	private static final Logger logger = LoggerFactory
			.getLogger(DataSourceConfig.class);

	@Autowired
	private Environment env;

	@Primary
	@Bean(name = "sqlJdbcDataSource")
	@Qualifier("sqlJdbcDataSource")
	public DataSource dataSource2() {
		DataSource dataSource = new DataSource();
		dataSource.setUrl(env.getProperty("spring.sqlserver.url"));
		dataSource.setDriverClassName(env
				.getProperty("spring.sqlserver.driver-class-name"));
		dataSource.setUsername(env.getProperty("spring.sqlserver.username"));
		dataSource.setPassword(env.getProperty("spring.sqlserver.password"));
		logger.info("======SQL Server DataSource success=====");
		return dataSource;
	}

	@Bean(name = "sqlserver")
	public JdbcTemplate sqlJdbcTemplate(
			@Qualifier("sqlJdbcDataSource") DataSource dataSource) {
		return new JdbcTemplate(dataSource);
	}

	@Bean(name = "oracleJdbcDataSource")
	@Qualifier("oracleJdbcDataSource")
	public DataSource oracleDataSource() {
		DataSource dataSource = new DataSource();
		dataSource.setUrl(env.getProperty("datasource.oracle.url"));
		dataSource.setDriverClassName(env
				.getProperty("datasource.oracle.driver"));
		dataSource.setUsername(env.getProperty("datasource.oracle.username"));
		dataSource.setPassword(env.getProperty("datasource.oracle.password"));
		logger.info("======oracle DataSource success=====");
		return dataSource;
	}

	@Bean(name = "oracle")
	public JdbcTemplate oracleJdbcTemplate(
			@Qualifier("oracleJdbcDataSource") DataSource dataSource) {
		return new JdbcTemplate(dataSource);
	}
}